/*
 * DistributionDialog.java
 *
 * Created on Streda, 2007, november 7, 9:51
 */

package gui;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.LinkedList;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import logic.distributions.AbstractGenerator;
import logic.distributions.ConstantGenerator;
import logic.distributions.UniformGenerator;

/**
 *
 * @author  alvin
 */
public class DistributionDialog extends javax.swing.JDialog {
    private AbstractGenerator gen = null;
    private Vector<Object> backup;
    
    public interface DistributionParametersListener {
        public void parametersChanged(AbstractGenerator g);
    }
    
    private LinkedList<DistributionParametersListener> dpl = 
            new LinkedList<DistributionParametersListener>();
    public void addParametersChangeListener(DistributionParametersListener newListener) {
        dpl.add(newListener);
    }
    
    private void fireParametersChanged()  {
        for (DistributionParametersListener listener : dpl)
            listener.parametersChanged(gen);
    }
    
    /**
     * Creates new form DistributionDialog
     */
    public DistributionDialog(JFrame owner) {
        super(owner, true);
        initComponents();
        gen = null;

        setDistribution(new ConstantGenerator());
    }

    public void setDistribution(AbstractGenerator g) {
        setLocationRelativeTo(this.getOwner());
        gen = g;
        backup = new Vector<Object>(gen.getParamsCount());
        for (int i = 0; i < gen.getParamsCount(); i++)
            backup.add(gen.getParameter(i));
        showValues();
        mainPanel.revalidate();
        repaint();
    }

    private void useBackup() {
        for (int i = 0; i < gen.getParamsCount(); i++) {
            try {
                gen.setParameter(i, backup.get(i));
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
    
    public void showValues() {
        mainPanel.removeAll();
        if (gen == null)
            return;
        
        JLabel popis = null;
        JTextField hodnota = null;
        for (int i = 0; i < gen.getParamsCount(); i++) {
            popis = new JLabel(gen.getParameterName(i));
            
            hodnota = new JTextField(gen.getParameter(i).toString());
            hodnota.setMinimumSize(new Dimension(100, 19));
            hodnota.setPreferredSize(new Dimension(100,19));
            
            ParameterListener pl = new ParameterListener(i);
            hodnota.addActionListener(pl);
            hodnota.addFocusListener(pl);
            

            JPanel pomPanel = new JPanel();
            pomPanel.setLayout(new FlowLayout());
            pomPanel.add(popis);
            pomPanel.add(hodnota);

            mainPanel.add(pomPanel);
        }

        mainPanel.setMinimumSize(new Dimension(100,100));
        mainPanel.setPreferredSize(new Dimension(100,100));
        setPreferredSize(new Dimension(100,100));
        setMinimumSize(new Dimension(200,150));
        
        JButton okBtn = new JButton("OK");
        okBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                fireParametersChanged();
                setVisible(false);
            }
        });
        JButton cancelBtn = new JButton("Cancel");
        cancelBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                useBackup();
                setVisible(false);
            }
        });

        JPanel pom2 = new JPanel();
        pom2.setLayout(new FlowLayout());
        pom2.add(okBtn);
        pom2.add(cancelBtn);
        mainPanel.add(pom2);
    }
    
    private class ParameterListener implements ActionListener, FocusListener{
        private int index;
        public ParameterListener(int paramIndex) {
            super();
            index = paramIndex;
        }
        
        private void setParam(JTextField pole) {
            String valueOK = gen.checkParameter(index, pole.getText());
            if (valueOK != null) {
                pole.setText(gen.getParameter(index).toString());
                JOptionPane.showMessageDialog(null,
                        valueOK,
                        "ERROR",
                        JOptionPane.ERROR_MESSAGE);
                return;
            }
            try {
                gen.setParameter(index, pole.getText());
            } catch (Exception ex) {
                //tu uz doslo ku ozajstnej chybe, pretoze checkParameter
                //presiel spravne
                ex.printStackTrace();
            }
        }
        
        public void actionPerformed(ActionEvent e) {
            setParam((JTextField)e.getSource());
        }

        public void focusGained(FocusEvent e) {
        }

        public void focusLost(FocusEvent e) {
            setParam((JTextField)e.getSource());
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        mainPanel = new javax.swing.JPanel();

        setMinimumSize(new java.awt.Dimension(100, 100));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        mainPanel.setMinimumSize(new java.awt.Dimension(100, 100));
        getContentPane().add(mainPanel, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        useBackup();
    }//GEN-LAST:event_formWindowClosing
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel mainPanel;
    // End of variables declaration//GEN-END:variables
    
}
